local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());
local PetTool=_G.CommonOHOH.load.PetTool();
local EventList = ServerOHOH.load.EventList();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local Egg_Cfg=_G.CommonOHOH.config.Egg
local ItemTool=_G.CommonOHOH.load.ItemTool();

function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);    
    self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
    self.player=parent
    self.HatchEggEvent=self.player.HatchEggEvent
    self.PetEuqipUpdateEvent=self.player.PetEuqipUpdateEvent

    
end;

function M.dtor(self)
	M.super.dtor(self);
end;


--获取摇宠物的次数幸运值
function M.GetPetLuckyCount(self)
    s=0
    local arg=self.parent.PetLuckyEvent:Action()
    if arg then
        for _,v in pairs(arg) do
            s=s+v
        end
    end

    return s
end

function M.GetEquipCount(self,id,count,DeletePetIds)
    local count=0
    for k,v in pairs(self.DataBase:GetData("pets").equip.__real) do
        count=count+v
    end
    return count
end

--基础摇蛋
function M.BaseHatch(self,id,count,DeletePetIds)

    -- print("-----BaseHatch------",id)

    self.player.OnboardingFunnelEvent:Action("egg") --统计：抽蛋
    
    DeletePetIds=DeletePetIds or {}

    id=tonumber(id)

    local cfg=Egg_Cfg[id]
    assert(cfg,"eggId:"..id)
    
    local petIdList={}
    local send2ClientPetId={}
    --开始摇蛋
    for i=1,count do
        local index=GeneralTool.RandomLucky(cfg.probability,self:GetPetLuckyCount())
        local petId= PetTool.GetPetId(cfg.petIds[index])
        if not DeletePetIds[petId] then
            table.insert(petIdList,petId)
        end
        table.insert(send2ClientPetId,petId)
    end

    -- 已经装备宠物的数量
    local hadEquip=self:GetEquipCount()
    -- 最大装备宠物数量
    local maxEquip=self.DataBase:GetData("petsPack").equipMax

    local isEquipPet=false
    --存档
    local petsData=self.DataBase:GetData("pets")
    for _,petId in pairs(petIdList) do

        if petsData.had.__real[petId] then
            petsData.had[petId]=petsData.had[petId]+1
        else
            petsData.had[petId]=1
        end
        if hadEquip<maxEquip then

            if not petsData.equip[petId] then
                petsData.equip[petId]=1
            else
                petsData.equip[petId]=petsData.equip[petId]+1
            end
           
            isEquipPet=true

            hadEquip=hadEquip+1
        end
    end

    if isEquipPet  then
        self.PetEuqipUpdateEvent:Action()
    end

    self.DataBase:Execut(function()

	end);


    self.HatchEggEvent:Action(id)
    -- print("-----BaseHatch-----22-",id)

    self:Fire(EventList.hatch_egg,send2ClientPetId,id,count,DeletePetIds);

end

--普通摇蛋
function M.CommonHatch(self,eggId,count,DeletePetIds)--摇的数量

    -- print("-----CommonHatch------",eggId)
    local cfg=Egg_Cfg[eggId]
    assert(cfg,"No Egg ID:"..eggId)
    --判断背包
    if not PetTool.IsCanPlacedPet(count,self.DataBase) then
        return 
    end

    local cost=count*cfg.arg

    --判断钱够不够
    if  not ItemTool.IsHadEnough(cfg.costId,cost,self.DataBase) then
        return false
    end

    ItemTool.SaveData(cfg.costId,self.DataBase,-cost)

    self:BaseHatch(eggId,count,DeletePetIds)

    --同步数据
    self.parent:SynchDataToClient("pets","base")

end;

--免费摇蛋
function M.FreeHatch(self,eggId,count,DeletePetIds)
    --判断背包
    if not PetTool.IsCanPlacedPet(count,self.DataBase) then
        return 
    end
    self:BaseHatch(eggId,count,DeletePetIds)
    self.parent:SynchDataToClient("pets")
end


M:RegistEvent(EventList.CommonHatch,function(self,...)
    self:CommonHatch(...)
end)

M:RegistEvent(EventList.FreeHatch,function(self,...)
    self:FreeHatch(...)
end)



return M